package com.nanhu.test.pager;

import java.util.ArrayList;

import com.nanhu.test.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;

public class PagerOneActivity extends Activity{
	
	ArrayList<View> pageViews;
	
	ImageView[] imageViews;
	
	private ViewGroup main;
	
	private ViewGroup group;
	
	private ViewPager viewPager;
	
	private ImageView imageView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		LayoutInflater inflater = getLayoutInflater();
		pageViews = new ArrayList<View>();
		pageViews.add(inflater.inflate(R.layout.item05, null));
        pageViews.add(inflater.inflate(R.layout.item06, null));
        pageViews.add(inflater.inflate(R.layout.item01, null));  
        pageViews.add(inflater.inflate(R.layout.item02, null));  
        pageViews.add(inflater.inflate(R.layout.item03, null));  
        pageViews.add(inflater.inflate(R.layout.item04, null));
        
        imageViews = new ImageView[pageViews.size()];
        main = (ViewGroup)inflater.inflate(R.layout.pager_one, null);
        group = (ViewGroup)main.findViewById(R.id.viewGroup);
        viewPager = (ViewPager)main.findViewById(R.id.guidePagers);
        
        for(int i = 0; i < pageViews.size(); i++){
        	imageView = new ImageView(PagerOneActivity.this);
        	imageView.setLayoutParams(new LayoutParams(20, 20));
        	imageView.setPadding(20, 0, 20, 0);
        	imageViews[i] = imageView;
        	if(i == 0){
        		imageView.setBackgroundResource(R.drawable.page_indicator_focused);
        	}else{
        		imageView.setBackgroundResource(R.drawable.page_indicator);
        	}
        	group.addView(imageView);
        }
        
        setContentView(main);
        viewPager.setAdapter(new GuidePageAdapter());
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());
	}
	
	class GuidePageAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		
		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}
		
		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager)container).removeView(pageViews.get(position));
		}
		
		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(pageViews.get(position));
			return pageViews.get(position);
		}
		
		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
			super.restoreState(state, loader);
		}
		
		@Override
		public Parcelable saveState() {
			return super.saveState();
		}
		
		@Override
		public void startUpdate(View container) {
			super.startUpdate(container);
		}
		
		@Override
		public void finishUpdate(View container) {
			super.finishUpdate(container);
		}
	}
	
	class GuidePageChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int arg0) {
			for(int i = 0; i < imageViews.length; i++){
				if(i == arg0){
					imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
				}else{
					imageViews[i].setBackgroundResource(R.drawable.page_indicator);
				}
			}
		}
		
	}
}
